package com.zryc.shop.ums.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zryc.shop.ums.entity.Role;
import com.zryc.shop.ums.service.IMenuService;
import com.zryc.shop.ums.service.IResourceService;
import com.zryc.shop.ums.service.IRoleService;
import com.zryc.shop.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 后台用户角色表 前端控制器
 * </p>
 *
 * @author LiYe
 * @since 2023-02-02
 */
@RestController
@RequestMapping("/ums/role")
@Api(tags = "角色管理接口列表")
public class RoleController {
    @Autowired
    private IRoleService roleService;

    @PostMapping("/create")
    @ApiOperation("创建新角色")
    public ResponseResult create(@RequestBody Role role) {
        role.setCreateTime(LocalDateTime.now());//设置创建日期和时间
        roleService.save(role);//保存这个角色
        return new ResponseResult(200, "创建成功");
    }

    @PostMapping("/update/{id}")
    @ApiOperation("修改角色信息")
    public ResponseResult update(@RequestBody Role role, @PathVariable @ApiParam(value = "角色ID", required = true) Long id) {
        role.setId(id);
        roleService.updateById(role);
        return new ResponseResult(200, "修改成功");
    }

    @GetMapping("/list")
    @ApiOperation("分页查询角色列表")
    public ResponseResult list(@RequestParam(required = false) @ApiParam(value = "角色名称") String keyword,
                               @RequestParam(defaultValue = "1") @ApiParam(value = "第几页", required = true) Integer pageNum,
                               @RequestParam(defaultValue = "5") @ApiParam(value = "每页显示几条", required = true) Integer pageSize) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(keyword))
            queryWrapper.eq("name", keyword);//精确查找，如果需要模糊查询将eq换成like即可
        Page page = new Page(pageNum, pageSize);
        roleService.page(page, queryWrapper);
        return new ResponseResult(200, "查询完成", page);
    }

    @GetMapping("/listAll")
    @ApiOperation("查询全部的角色")
    public ResponseResult listAll() {
        return new ResponseResult(200, "查询完成", roleService.list());
    }

    @PostMapping("/updateStatus/{id}")
    @ApiOperation("更新角色状态")
    public ResponseResult updateStatus(@PathVariable @ApiParam(value = "角色ID", required = true) Long id,
                                       @RequestParam @ApiParam(value = "角色状态：1-启用 0-禁用", allowableValues = "0,1") Integer status) {
        UpdateWrapper<Role> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", status);
        roleService.update(updateWrapper);
        return new ResponseResult(200, "修改成功");
    }

    @PostMapping("/delete")
    @ApiOperation("删除角色（支持批量删除）")
    public ResponseResult delete(@RequestParam @ApiParam(value = "角色ID列表", example = "1,2,3", required = true) String ids) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", ids.split(","));// WHERE id IN (1,2,3)
        roleService.remove(queryWrapper);
        return new ResponseResult(200, "删除成功");
    }

    @Autowired
    private IMenuService menuService;

    @GetMapping("/listMenu/{roleId}")
    @ApiOperation("查询该角色对应的菜单列表")
    public ResponseResult listMenu(@PathVariable @ApiParam(value = "角色ID", required = true) Long roleId) {
        return new ResponseResult(200, "查询完成", menuService.listByRoleId(roleId));
    }


    @PostMapping("/allocMenu")
    @ApiOperation("给角色分配菜单")
    public ResponseResult allocMenu(@RequestParam @ApiParam(value = "角色ID", required = true) Long roleId,
                                    @RequestParam @ApiParam(value = "菜单ID列表", required = true, example = "1,2,3")String menuIds) {
        roleService.allocMenu(roleId, menuIds);
        return new ResponseResult(200, "执行成功");
    }

    @Autowired
    private IResourceService resourceService;

    @GetMapping("/listResource/{roleId}")
    @ApiOperation("查询该角色对应的资源列表")
    public ResponseResult listResource(@PathVariable @ApiParam(value = "角色ID", required = true) Long roleId) {
        return new ResponseResult(200, "查询完成", resourceService.listByRoleId(roleId));
    }

    @PostMapping("/allocResource")
    @ApiOperation("给角色分配资源")
    public ResponseResult allocResource(@RequestParam @ApiParam(value = "角色ID", required = true) Long roleId,
                                        @RequestParam @ApiParam(value = "资源ID列表", required = true, example = "1,2,3")String resourceIds) {
        roleService.allocResource(roleId, resourceIds);
        return new ResponseResult(200, "执行成功");
    }


}
